Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor imports #109

Merged
merged 5 commits into from
Jan 8, 2019
Merged

Conversation

dnephin
Copy link
Contributor

@dnephin dnephin commented Dec 28, 2018

I believe in order to implement #103 properly 2 changes must be made:

  • types.TypeString needs to be called with types.RelativeTo to remove the package selector when the package matches the target package
  • the template needs to use types.TypeString for the line with var _ {{.TargetAlias}}.{{.TargetName}} =

This refactor should make it easier to implement both of these changes. It reduces the code required to handle imports, and I believe makes it easier to follow by doing de-duplication immediately, instead of a post-processing step, which required changing aliases and logic related to imports to
Index imports so that any duplicate aliase can be detected immediately before they are added.

Instead of hiding the types.TypeString call, implement a types.Qualifier which can be used to return the package alias. In a follow up PR this can be updated to make the call to types.RelativeTo.

Index imports so that any duplicate aliase can be detected
immediately before they are added.

Instead of hiding the types.TypeString call, implement a
types.Qualifier which can be used to return the package alias.
generator/import.go Outdated Show resolved Hide resolved
}

// AddImport creates an import with the given alias and path, and adds it to
// Fake.Imports.
func (f *Fake) AddImport(alias string, path string) Import {
func (i *Imports) Add(alias string, path string) Import {
// TODO: why is there extra whitespace on these args?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand; which white space / args are you referring to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path and alias are both passed to strings.TrimSpace so I assume there is either leading or trailing whitespace on those strings, but it is not clear to me where that would come from. It seems like this is called with values from inspecting go/types, which I would assume do not contain extra whitespace.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I am just always defensive with function arguments (particularly when exported).

unvendor was a copy of this function. x/tools/imports is already
a dependency of this package.
@joefitzgerald joefitzgerald merged commit bd876ff into maxbrunsfeld:master Jan 8, 2019
@dnephin dnephin deleted the refactor-imports branch January 8, 2019 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants